﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
    public class CGD_Dao
    {
        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(CGD entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                context.CGDSet.AddObject(entity);
                return context.SaveChanges();
            }
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Update(CGD entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                CGD entity2 = context.CGDSet.FirstOrDefault(c => c.ID == entity.ID);
                if (entity2 != null)
                {
                    //entity2 = entity;
                    entity.CJSJ = entity2.CJSJ;
                    context.CGDSet.ApplyCurrentValues(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                CGD entity = context.CGDSet.FirstOrDefault(c => c.ID == guid);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public CGD Query(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                CGD entity = context.CGDSet.FirstOrDefault(c => c.ID == guid);
                return entity;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<CGD> QueryAll()
        {
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.CGDSet
                        select c;
                List<CGD> list = new List<CGD>();
                if (q.Count() > 0)
                {
                    foreach (var c in q)
                    {
                        list.Add(c);
                    }
                }
                return list;
            }
        }

        public bool Confirm(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                CGD entity = context.CGDSet.FirstOrDefault(c => c.ID == guid);
                if (entity  != null)
                {
                    entity.ZT = 1;
                    context.CGDSet.ApplyCurrentValues(entity);
                    return context.SaveChanges() >= 1;
                }
                return false;
            }
        }

        public List<CGD> QueryBySql(string gysId, string departmentId, string userId, string datetimeStart, string datetimeEnd)
        {
            List<CGD> list = new List<CGD>();
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.CGDSet
                        select c;

                if (!string.IsNullOrEmpty(gysId) && (!"0".Equals(gysId)))
                {
                    Guid gid = Guid.Parse(gysId);
                    q = q.Where(c => c.GYSID == gid);
                }

                if (!string.IsNullOrEmpty(departmentId) && (!"0".Equals(departmentId)))
                {
                    Guid did = Guid.Parse(departmentId);
                    q = q.Where(c => c.BMID == did);
                }

                if (!string.IsNullOrEmpty(userId) && (!"0".Equals(userId)))
                {
                    Guid uid = Guid.Parse(userId);
                    q = q.Where(c => c.RYID == uid);
                }

                if ((!string.IsNullOrEmpty(datetimeStart)) && (!"0".Equals(datetimeStart)))
                {
                    DateTime start = DateTime.Parse(datetimeStart);
                    q = q.Where(c => (c.RQ >= start));
                }

                if ((!string.IsNullOrEmpty(datetimeEnd)) && (!"0".Equals(datetimeEnd)))
                {
                    DateTime end = DateTime.Parse(datetimeEnd);
                    q = q.Where(c => (c.RQ <= end));
                }

                foreach (CGD c in q)
                {
                    list.Add(c);
                }

                return list;
            }
        }
    }
}
